<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">养护半年报</div>
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline" style="width:100px">
                                    <select id="search-year">
                                        <option value="">选择年份</option>
                                    </select>
                                </div>
                                <div class="layui-inline" style="width:100px">
                                    <select id="search-semi">
                                        <option value="">选择上下年</option>
                                    </select>
                                </div>
                                <div class="layui-inline">
                                    <input id="search-date-range" type="text" placeholder="选择日期范围"
                                           class="layui-input">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-create-by-name" type="text"
                                           placeholder="请输入创建人" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline" style="width:100px">
                                    <select id="search-state">
                                        <option value="">报表状态</option>
                                        <option value="0">待提交</option>
                                        <option value="1">已提交</option>
                                    </select>
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="float:right;">
                            <a id="btn-add" class="layui-btn">添加</a>
                            <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="semi-report-table" lay-filter="semi-report-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        {{# if(d.state==0){ }}
                        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="edit">编辑报表</a>
                        <a class="layui-btn layui-btn-xs" lay-event="submit">提交</a>
                        {{# } }}
                        {{# if(d.state==1){ }}
                        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="preview">查看报表</a>
                        {{# } }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="semi-report-form">
    <div class="layui-card">
        <div class="layui-card-header">工作半年报</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="semi-report-form">
                <input type="hidden" name="id">
                <input id="file-id" type="hidden" name="fileId">
                <div class="layui-form-item">
                    <label class="layui-form-label">*年份</label>
                    <div class="layui-input-block">
                        <select id="year" name="year">
                            <option value="">选择年份</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*上下年</label>
                    <div class="layui-input-block">
                        <select id="semi" name="semi">
                            <option value="">选择上下年</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文件来源</label>
                    <div class="layui-input-block">
                        <select id="source" name="source" lay-filter="source">
                            <option value="">文件来源</option>
                            <option value="1" selected>模板</option>
                            <option value="2">上传</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="file-layer" style="display: none;">
                    <label class="layui-form-label">文件上传</label>
                    <div class="layui-input-block">
                        <div class="layui-upload-drag" style="display: block;" id="file-upload">
                            <i class="layui-icon layui-icon-upload"></i>
                            <div>点击上传，或将文件拖拽到此处</div>
                        </div>
                        <a id="file-down"></a>
                    </div>
                </div>

                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="form-submit">确定</button>
                </div>
                <div style="height:300px;"></div>
            </form>
        </div>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'laydate', 'upload'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var popupIndex;

        laydate.render({
            elem: '#search-date-range',
            range: true
        });

        loadYears($('#search-year'));

        loadSemis($('#search-semi'));

        //近三年
        function loadYears($elem) {
            for (var i = 0; i < 3; i++) {
                var year = new Date().getFullYear() - i;
                $elem.append('<option value="' + year + '">' + year + '</option>');
            }
            form.render();
        }

        //52个周
        function loadSemis($elem) {
            $elem.append('<option value="1">上半年</option>');
            $elem.append('<option value="2">下半年</option>');
            form.render();
        }

        table.render({
            elem: '#semi-report-table',
            url: 'semi-reports',
            title: '',
            skin: 'line',
            cols: [[
                {
                    title: '序号', width: 60, templet: function (d) {
                        return d.LAY_TABLE_INDEX + 1;
                    }
                },
                {field: 'reportCycle', title: '报表周期'},
                {field: 'startDate', title: '开始日期'},
                {field: 'endDate', title: '截止日期'},
                {field: 'editDate', title: '编制日期'},
                {field: 'createByName', title: '创建人'},
                {field: 'stateText', title: '状态', width: 100},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation'}
            ]],
            page: true
        });

        function getSemiOfYear() {
            const now = new Date();
            return Math.floor(now.getMonth() / 6) + 1;
        }

        function showReportForm() {
            popupIndex = admin.popupRight({
                area: '360px',
                success: function () {
                    var formHtml = $('#semi-report-form').html();
                    $('#' + this.id).html(formHtml);
                    form.render();
                    loadYears($('#year'));
                    loadSemis($('#semi'));
                    $('#year').val(new Date().getFullYear());
                    var semi = getSemiOfYear();
                    $('#semi').val(semi);
                    console.log('semi', semi);
                    form.render();
                    form.on('select(source)', function (data) {
                        var value = data.value;
                        if (value == 1) {
                            $('#file-layer').hide();
                        } else if (value == 2) {
                            $('#file-layer').show();
                        }
                    });
                    upload.render({
                        elem: '#file-upload',
                        url: '/office/upload',
                        accept: 'file',
                        exts: 'xls|xlsx|doc|docx',
                        done: function (res) {
                            var data = res.data;
                            $('#file-upload').hide();
                            $('#file-down').attr("href", "/office/download/" + data.storageName);
                            $('#file-down').text(data.fileId);
                            $('#file-id').val(data.storageName);
                        }
                    });
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search() {
            table.reload('semi-report-table', {
                where: {
                    year: $('#search-year').val(),
                    semi: $('#search-semi').val(),
                    dateRange: $('#search-date-range').val(),
                    createByName: $('#search-create-by-name').val(),
                    state: $('#search-state').val()
                },
                page: {
                    curr: 1
                }
            });
        }

        $(document).on('keypress', function (e) {
            if (e.which == 13) {
                search();
            }
        });

        $("#btn-search").on('click', function () {
            search();
        });

        $("#btn-add").on('click', function () {
            popupIndex = showReportForm();
        });

        $("#btn-refresh").on('click', function () {
            table.reload('semi-report-table');
            layer.msg('表格已刷新');
        });

        table.on('tool(semi-report-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'semi-reports/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('semi-report-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'preview') {
                window.open('office-viewer?fileId=' + data.fileId);
            } else if (obj.event === 'edit') {
                window.open('office-editor?fileId=' + data.fileId);
            } else if (obj.event === 'submit') {
                layer.confirm('确定提交吗？', function (index) {
                    $.ajax({
                        url: 'semi-reports/submit',
                        type: 'POST',
                        data: {id: data.id},
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('提交成功');
                                table.reload('semi-report-table');
                            } else {
                                layer.msg('提交失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        form.on('submit(form-submit)', function (data) {
            var loadIndex = layer.msg('正在生成报表请稍后...', {
                icon: 16,
                shade: 0.01
            });
            $.ajax({
                url: 'semi-reports/create',
                type: 'POST',
                data: data.field,
                success: function (result) {
                    layer.close(loadIndex);
                    if (result.code == 200) {
                        layer.msg('操作成功');
                        table.reload('semi-report-table');
                    } else {
                        layer.msg(result.msg);
                    }
                }
            });
            closePopupForm();
            return false;
        });

    });
</script>
</body>
</html>